/**
 * Definition for a binary tree node.
 * function TreeNode(val, left, right) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.left = (left===undefined ? null : left)
 *     this.right = (right===undefined ? null : right)
 * }
 */
/**
 * @param {number[]} nums
 * @return {TreeNode}
 */
var constructMaximumBinaryTree = function(nums) {
    const createTree = function(l, r) {
        let node = new TreeNode()
        if(l < r) {
            return null
        }
        let mid = l, maxVal = nums[l]
        for(let i = l; i <= r; i++) {
            if(nums[i] > maxVal) {
                mid = i
                maxVal = nums[i]
            }
        }
        node.val = maxVal
        node.right = createTree(mid + 1, r)
        node.left =  createTree(l, mid - 1)
        return node
    }
    return createTree(0, nums.length - 1)
};